<script>
import { setTimeout } from 'timers'
export default {
  onLaunch: function () {
    this.$db.clear()
    this.testLogin()
    setTimeout(() => {
      uni.hideTabBar()
      // this.AppLogin()
    }, 100)
  },
  onShow: function () {},
  onHide: function () {},
  methods: {
    testLogin() {
      uni.setStorageSync('wxAdd', {
        latitude: 32.115804,
        longitude: 118.820313,
        address: '测试地址'
      })

      let obj = {
        userid: '664576186887380992',
        username: '176****9901',
        userTokenKey:
          'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJodHRwOi8vbWVtYmVyLWNlbnRlcjo5NTAxL2F1dGgvc2lnbkluL3F1aWNrIiwiaWF0IjoxNzE5OTY5MDQzLCJleHAiOjE3MTk5NzI2NDMsIm5iZiI6MTcxOTk2OTA0MywianRpIjoibFFSZ2t4c0xGSlJBSzBxWCIsInN1YiI6IjY2NjkzMjI2ODUyNjkxMTQ4OCIsInBydiI6IjM0ZDUxYWVhYWJiZjQzODBhOGQyMTM0ZDBkNzI3MmZiMjk3ZDUxY2UifQ.FhTEv0PdYZ7lPVfDzz1hohZPserasAgVsGPZu7mHVdC38dWuD_F4R0oBR9zsQrSdfjCavCHQynPbgbwNlk07UQ',
        telephone: '17634869901'
      }

      let username = obj.member_name || obj.username
      let obj1 = {
        userid: obj.userid,
        username,
        userTokenKey: obj.userTokenKey,
        telephone: obj.telephone
      }
      this.$http({
        url: 'login',
        method: 'post',
        data: {
          ...obj1
        }
      })
        .then(res => {
          this.$db.setUser(res.data)
          this.$db.setToken(res.data.token)
        })
        .catch(err => {
          uni.showModal({
            title: '提示',
            content: err.msg,
            showCancel: false
          })
        })
    },
    AppLogin() {
      var uniMPModule = uni.requireNativePlugin('HGUniMPModule')
      uniMPModule.getAppLocationInfoCallback(res => {
        const address = res && JSON.parse(res)
        uni.setStorageSync('wxAdd', address)
      })
      uniMPModule.getUserInfo(
        {
          needlogin: '1'
        },
        res => {
          const user = JSON.parse(res)
          // setTimeout(() => {
          //   uni.showModal({
          //     content: res,
          //     title: 'sdk获取的个人信息'
          //   });
          // }, 1000);
          // 你保留userid,member_name/username(安卓跟IOS不一样),userTokenKey
          let username = user.member_name || user.username
          let obj = {
            userid: user.userid,
            username,
            userTokenKey: user.userTokenKey,
            telephone: user.telephone
          }
          this.$http({
            url: 'login',
            data: {
              ...obj
            }
          })
            .then(res => {
              // setTimeout(() => {
              //   uni.showModal({
              //     content: JSON.stringify(res),
              //     title: '接口返回'
              //   })
              // }, 1500)

              this.$db.setUser(res.data)
              this.$db.setToken(res.data.token)
            })
            .catch(err => {
              this.$db.clear()
            })
        }
      )
    }
  }
}
</script>

<style>
/* @import 'uni.scss'; */
.noneData {
  width: 95%;
  height: 200px;
  background: url('./static/img/nonedata.png') no-repeat center;
  background-size: contain;
  margin: 20px auto;
}

/*每个页面公共css */
</style>
